﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DLL.FileContent
{
	/// <summary>
	/// 控制类
	/// </summary>
	/// <![CDATA[2014-8-17 11:41:21]]>
	public class Component : DLL.ComponentBase
	{
		#region 属性
		/// <summary>
		/// 问题数据类对象
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		private Database clsDatabase;		// 数据类对象
		/// <summary>
		/// 配置类对象
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		private Config clsConfig = new Config();		// 配置类对象
		#endregion 属性

		#region 构造函数
		/// <summary>
		/// 控制类构造函数
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		public Component(System.Web.UI.Page page)
			: base(page)
		{
			clsDatabase = new Database(page);
			clsData = clsDatabase;

			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nID", clsDatabase.ID, clsDatabase.listStructure[clsDatabase.ID].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("int", "nStatus", clsDatabase.STATUS, clsDatabase.listStructure[clsDatabase.STATUS].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strType", clsDatabase.TYPE, clsDatabase.listStructure[clsDatabase.TYPE].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strFileName", clsDatabase.FILENAME, clsDatabase.listStructure[clsDatabase.FILENAME].strName));
			listDatabaseEntity.Add(new TonyNet4V1.Structs.DatabaseAndEntity("string", "strDateTime", clsDatabase.DATETIME, clsDatabase.listStructure[clsDatabase.DATETIME].strName));

			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nID", clsDatabase.listStructure[clsDatabase.ID].strName, "="));
			listQueryData.Add(new TonyNet4V1.Structs.QueryData("int", "nStatus", clsDatabase.listStructure[clsDatabase.STATUS].strName, "="));
		}
		#endregion 构造函数

		#region 类型转换
		protected override TonyNet4V1.EntityBase GetEntity()
		{
			return new Entity();
		}
		protected override TonyNet4V1.QueryEntityBase GetQuery()
		{
			return new QueryEntity();
		}
		protected override TonyNet4V1.DataBase GetData()
		{
			return new Database(page);
		}
		/// <summary>
		/// 把查询结果链表中的实体类型转为现在的实体类型
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		/// <param name="list"></param>
		/// <returns></returns>
		private List<Entity> ConvertListEntityType(List<TonyNet4V1.EntityBase> list)
		{
			List<Entity> listEntity = new List<Entity>();

			for (int i = 0; i < list.Count; i++)
			{
				Entity clsEntity = new Entity();

				clsEntity = (Entity)list[i];

				listEntity.Add(clsEntity);
			}

			return listEntity;
		}
		#endregion 类型转换

		#region 数据实体转换
		
		protected override TonyNet4V1.EntityBase DataToEntity(System.Data.DataRow dr)
		{
			Entity clsEntity = new Entity();

			if (dr != null)
			{
				clsEntity = (Entity) base.DataToEntity(dr);

				clsEntity.Content = Convert.FromBase64String(dr[clsDatabase.CONTENT].ToString());
				return clsEntity;
			}
			else
			{
				return null;
			}
		}

		protected override void EntityToData(TonyNet4V1.EntityBase clsEntity)
		{
			base.EntityToData(clsEntity);

			clsDatabase.listStructure[clsDatabase.CONTENT].strValue = Convert.ToBase64String(((Entity)clsEntity).Content);
		}

		#endregion 数据实体转换


		#region 添加、修改等
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-17 14:33:57]]>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public bool Add(ref Entity clsEntity)
		{
			Entity clsAdd = new Entity();

			if (base.Add(clsAdd))
			{
				clsAdd = (Entity) GetNewest();

				clsEntity.ID = clsAdd.ID;

				//clsAdd.Content = clsEntity.Content;
				//clsAdd.DateTime = clsEntity.DateTime;
				//clsAdd.FileName = clsEntity.FileName;
				//clsAdd.Type = clsEntity.Type;

				return Modify(clsEntity);
			}
			else
			{
				return false;
			}

		}
		/// <summary>
		/// 添加
		/// </summary>
		/// <![CDATA[2014-8-17 14:44:35]]>
		/// <param name="fUpload"></param>
		/// <returns></returns>
		public Entity Add(System.Web.UI.WebControls.FileUpload fUpload)
		{
			Entity clsEntity = new Entity();
			byte[] bContent;

			if (fUpload.HasFile)
			{
				clsEntity.FileName = fUpload.FileName;
				clsEntity.DateTime = System.DateTime.Now.ToString();
				clsEntity.Type = fUpload.PostedFile.ContentType;
				bContent = new byte[fUpload.PostedFile.ContentLength - 1];
				bContent = fUpload.FileBytes;
				clsEntity.Content = bContent;

				//return base.Add(clsEntity);
				if (Add(ref clsEntity))
				{
					return clsEntity;
				}
				else
				{
					return null;
				}
			}
			else
			{
				return null;
			}
			
		}

		/// <summary>
		/// 修改
		/// </summary>
		/// <param name="fUpload"></param>
		/// <param name="clsEntity"></param>
		/// <returns></returns>
		public bool Modify(System.Web.UI.WebControls.FileUpload fUpload, Entity clsEntity)
		{
			byte[] bContent;

			if (fUpload.HasFile)
			{
				clsEntity.FileName = fUpload.FileName;
				clsEntity.DateTime = System.DateTime.Now.ToString();
				clsEntity.Type = fUpload.PostedFile.ContentType;
				bContent = new byte[fUpload.PostedFile.ContentLength - 1];
				bContent = fUpload.FileBytes;
				clsEntity.Content = bContent;
			}

			return Modify(clsEntity);
		}

		#endregion 添加、修改等

		#region 查询
		/// <summary>
		/// 根据ID获得实体对象
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		/// <param name="nID"></param>
		/// <returns></returns>
		public new Entity GetOne(int nID)
		{
			return (Entity)base.GetOne(nID);
		}

		/// <summary>
		/// 查询所有
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		/// <returns></returns>
		public new List<Entity> QueryAll()
		{
			return ConvertListEntityType(base.QueryAll());
		}

		/// <summary>
		/// 查询
		/// </summary>
		/// <![CDATA[2014-8-17 11:41:21]]>
		/// <returns></returns>
		public List<Entity> Query(QueryEntity clsQuery)
		{
			return ConvertListEntityType(base.Query(clsQuery));
		}
		#endregion 查询
	}
}
